package Thread;

/**
 * @author liyang
 * @version 1.0
 * @package Thread
 * @file SyncDemo4.java
 * @description
 * @time 2024-04-12 16:23
 */
public class SyncDemo4 {
    public static void main(String[] args) {
        /*尽管t1,t2启动的是不同的方法，但因为他们都被锁，且在同一个对象Boo下，所以执行的时候
        * 会出现互斥*/
        Boo boo = new Boo();
        Thread t1 = new Thread(){
            @Override
            public void run() {
                boo.MethodA();
            }
        };

        Thread t2 = new Thread(){
            @Override
            public void run() {
                boo.MethodB();
            }
        };

        t1.start();
        t2.start();

    }
}
class Boo {
    public synchronized void MethodA(){
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+":执行A方法");
            Thread.sleep(5000);
            System.out.println("A结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void MethodB(){
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+":执行B方法");
            Thread.sleep(5000);
            System.out.println("B结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
